//
//  VideoInfoVC.m
//  Brand
//
//  Created by Faker on 5/4/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "VideoInfoVC.h"
#import "VideoObj.h"
#import "VideoViewController.h"
#import "VideoDetailVC.h"
#import "Header.h"
#import "config.h"

@interface VideoInfoVC ()
@property (weak, nonatomic) IBOutlet UIView *navTopContentView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet UIButton *shareBtn;

@property (assign, nonatomic) float setHeight;



@end

@implementation VideoInfoVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self drawNavTopContentView:self.navTopContentView];
    [self setUp];
}

-(void)setUp
{
    self.titleLabel.text = self.videoObj.contentName;
   
    self.myScrollView.contentHeight = self.myScrollView.height+1;
    [self btnsDisable];
    [self prepareGetData];
}

#pragma mark - 网络请求
-(void)prepareGetData
{
    [kApp displayWaitViewWithAllMaskView:NO];
    
    NSString* url = [NSString stringWithFormat:@"%@videoIntro&id=%@",API_BASE,self.videoObj.episodeId];
    
    [[AFGetData sharedInstance] startGet:url andResultIsJSON:YES andRequestCompletion:^(id responseObj, BOOL isSuccess) {
        if(isSuccess)
        {
            [self getDataSuccess:responseObj];
        }
        else{
            [self getDataError];
        }
    }];
}

-(void)getDataSuccess:(id)reponseObj
{
    [kApp hideWaitView];
    [self btnEnable];
    
    NSDictionary* dic = (NSDictionary*)reponseObj;
    
    UILabel* directorLabel = (UILabel*)[self.myScrollView viewWithTag:203];
    UILabel* roleLabel = (UILabel*)[self.myScrollView viewWithTag:204];
    UILabel* areaLabel = (UILabel*)[self.myScrollView viewWithTag:205];
    UILabel* typeLabel = (UILabel*)[self.myScrollView viewWithTag:206];
    UILabel* dateLabel = (UILabel*)[self.myScrollView viewWithTag:207];
    UILabel* producerLabel = (UILabel*)[self.myScrollView viewWithTag:208];
    UILabel* publisherLable = (UILabel*)[self.myScrollView viewWithTag:209];
    
    
    self.videoObj.episodeId = [dic objectForKey:@"id"];
    self.videoObj.type = [dic objectForKey:@"vtype"];
    self.videoObj.director = directorLabel.text = [dic objectForKey:@"director"];
    self.videoObj.role = roleLabel.text = [dic objectForKey:@"swriter"];
    self.videoObj.area = areaLabel.text = [dic objectForKey:@"region"];
    self.videoObj.category = typeLabel.text = [dic objectForKey:@"type"];
    self.videoObj.producer = producerLabel.text = [dic objectForKey:@"producer"];
    self.videoObj.publisher = publisherLable.text = [dic objectForKey:@"publisher"];
    self.videoObj.releaseDate = dateLabel.text = [dic objectForKey:@"release_date"];
    self.videoObj.intro = [dic objectForKey:@"desc"];
    self.videoObj.imageSrc = [dic objectForKey:@"pic"];
    self.videoObj.isReadyShow = [[dic objectForKey:@"is_ok"] boolValue];
    if(self.videoObj.isReadyShow)
    {
        self.playBtn.enabled = YES;
    }
    else{
        self.playBtn.enabled = NO;
    }
    
   
    float oldHeight = self.infoLabel.height;
    float newHeight  = [Common handleTheLabelHeight:self.infoLabel withText:self.videoObj.intro withMaxHeight:5000];
    self.setHeight = newHeight - oldHeight;
    self.myScrollView.contentHeight += self.setHeight>=0?self.setHeight:0;
    [[AFGetData sharedInstance] initSDWebImage:self.videoObj.imageSrc andTrueImageView:self.imageView andPlaceholderImage:nil];
}

-(void)getDataError
{
    [kApp hideWaitView];
    [PopTips tips:self.view Msg:NotifyNoNet];
    [self performSelector:@selector(goBack:) withObject:self afterDelay:0.7];
    return;
}

#pragma mark - 按钮的可点与不可点
-(void)btnsDisable
{
    self.shareBtn.enabled = NO;
    self.playBtn.enabled = NO;
}

-(void)btnEnable
{
    self.shareBtn.enabled = YES;
    self.playBtn.enabled = YES;
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - xib点击事件
- (IBAction)goBack:(id)sender {
    [kApp hideWaitView];
    [[AFGetData sharedInstance] removeAllGetAndPostRequest];
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)goToShare:(id)sender {
    [kApp initShareViewWithUrl:ShareUrl andWithTitle:ShareTitle andWithInfo:NotifyRecommendShare andWithImageUlr:ShareImageUrl andIsSuccess:^(BOOL isSuccess) {
        if(isSuccess)
        {
            [PopTips tips:kApp.rootNav.view Msg:NotifyShareSuccess];
        }
        else{
            [PopTips tips:kApp.rootNav.view Msg:NotifyShareError];
        }
    }];

}

- (IBAction)rightPlayClick:(id)sender {
    VideoViewController* videoVC = [[VideoViewController alloc]initWithNibName:@"VideoViewController" bundle:nil];
    VideoDetailVC* videoDetailVC = [[VideoDetailVC alloc]initWithNibName:@"VideoDetailVC" bundle:nil];
    if([self.videoObj.type isEqualToString:@"1"])
    {
        videoDetailVC.videoObj = self.videoObj;
        [self.navigationController pushViewController:videoDetailVC animated:YES]; //电影
    }
    else{
        [self.navigationController pushViewController:videoVC animated:YES]; //周播剧
    }
    
}
@end
